import {createRouter, createWebHistory} from 'vue-router'
import store from '../store/index.js'
import Home from '../views/home/index.vue'
import Checkout from "../views/checkout/index.vue";
import Payment from "../views/payment/index.vue";
import Login from "../views/login/index.vue";
import { message } from 'ant-design-vue';

const routes = [
  {
	  path: '/',
	  redirect: '/login'
  },
  {
  	  path: '/login',
  	  name: 'Login',
	  component: Login
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
	meta: {requiresAuth:true}
  },
  {
    path: '/checkout',
    name: 'Checkout',
    component: Checkout,
	meta: {requiresAuth:true}
  },
  {
    path: '/payment',
    name: 'Payment',
    component: Payment,
	meta: {requiresAuth:true}
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes: routes
})

router.beforeEach((to,from,next) => {
	if(to.matched.some(record => record.meta.requiresAuth)){
		if(!store.state.islogin){
			next({name:'Login'})
			message.warning("无法跳转，请先登录")
		}
		else{
			next()
		}
	}
	else{
		next()
	}
})
export default router